require(['./config'],() =>{
    require(['header','footer','util'],() =>{
        class Login{
            constructor(){
                this.log()
            }
            log(){
                $('#login').on('click', () =>{
                this.username = $('#loginName').val()
                this.password = $('#loginPassword').val()
                this.userList = localStorage.getItem('userList')
                if (this.userList) {
                    this.userList = JSON.parse(this.userList)
                    // 判断是否有一条跟输入的用户名密码一致
                    const isSucc = this.userList.some(userinfo => {
                      return userinfo.username === this.username && userinfo.password === this.password
                    })
                    if (isSucc) {
                      // 登录成功
                      // 把用户名存cookie，从而在首页可以读取登录信息
                      util.setCookie('loginUsername', this.username, { path: '/' })
                      alert('登录成功，即将跳转首页')
                      window.location.replace('/')
                    } else {
                      alert('用户名或密码错误，请重试')
                    }
                  } else {
                    alert('请先注册')
                    window.open('/html/register.html')
                  }
                })
            }
        }
        new Login()
    })
})